package com.java.controller.api;


import com.java.hfb.RequestHelper;
import com.java.pojo.entity.LendItem;
import com.java.pojo.vo.InvestVo;
import com.java.result.Result;
import com.java.service.LendItemService;
import com.java.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 标的出借记录表 前端控制器
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */

@Api(tags = "标的的投资")
@RestController
@RequestMapping("/api/core/lendItem")
public class LendItemController {

    @Resource
    private LendItemService lendItemService;

    @ApiOperation("会员投资提交的数据")
    @PostMapping("/auth/commitLendItem")
    public Result commitLendItem(@RequestBody InvestVo investVo, HttpServletRequest request){
        String token = request.getHeader("token");
        //获取id 获取name 封装到vo类中
        Long userId = JwtUtils.getUserId(token);
        String name = JwtUtils.getUserName(token);
        investVo.setInvestUserId(userId);
        investVo.setInvestName(name);
        String formStr = lendItemService.commitLendItem(investVo);
        return Result.ok(formStr);
    }

    @ApiOperation("会员投资提交的数据的回调")
    @PostMapping("/notify")
    public String notify(HttpServletRequest request){
        Map<String, Object> map = RequestHelper.switchMap(request.getParameterMap());
        //签名校验
        if(RequestHelper.isSignEquals(map)){
            //判断充值是否成功
            if("0001".equals(map.get("resultCode"))){
                //同步
                lendItemService.Commitnotify(map);
            }else{
                //不管充值失败还是网络原因都返回success
                return "success";
            }
        }
        return "fail";
    }

    @ApiOperation("获取列表")
    @GetMapping("/list/{lendId}")
    public Result list(
            @ApiParam(value = "标的id", required = true)
            @PathVariable Long lendId) {
        List<LendItem> list = lendItemService.getByLendId(lendId);
        return Result.ok(list);
    }
}

